package com.powernode.config;

import cn.hutool.core.util.ObjectUtil;
import com.powernode.constant.AuthConstants;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * ClassName:FeignInterceptor
 * Package:com.powernode.config
 * Description:
 *
 * @date:2025/1/25 17:57
 * @author:bailihua
 */
@Component
public class FeignInterceptor implements RequestInterceptor {

    /**
     *
     * @param requestTemplate 即将要发起新的请求的请求对象
     */
    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 获取当前请求的上下文
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        //判断是否有值
        if(ObjectUtil.isNotNull(requestAttributes)){
            //获取请求对象
            HttpServletRequest request = requestAttributes.getRequest();
            //判断是否有值
            if(ObjectUtil.isNotNull(request)){
                //获取当前请求头中的token值，传递到下一个请求对象的请求头中
                String autorization  = request.getHeader(AuthConstants.AUTHORIZATION);
                requestTemplate.header(AuthConstants.AUTHORIZATION,autorization);
                return;
            }

        }

        // 将当前请求对象头里的token传递到下一个请求对象的请求头中
        requestTemplate.header(AuthConstants.AUTHORIZATION,AuthConstants.BEARER+"76fcb60a-578b-43ec-85a6-2cdfb1f08773");
    }
}
